package com.hcrazy.manchester.app.services;

import java.lang.reflect.Type;
import java.util.List;

import org.joda.time.DateTime;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hcrazy.manchester.app.jdbc.BookCustomerDAO;
import com.hcrazy.manchester.app.jdbc.BookOrderDAO;
import com.hcrazy.manchester.app.jdbc.SellerDAO;
import com.hcrazy.manchester.app.pojo.BookCustomer;
import com.hcrazy.manchester.app.pojo.BookOrder;
import com.hcrazy.manchester.app.pojo.BookOrderItem;
import com.hcrazy.manchester.app.pojo.BookOrderStatis;
import com.hcrazy.manchester.app.pojo.ErrorMsg;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.utils.Constant;

public class BookOrderService {

	private BookOrderDAO bookOrderDAO;
	
	private BookCustomerDAO bookCustomerDAO;
	
	private SellerDAO sellerDAO;

	public SellerDAO getSellerDAO() {
		return sellerDAO;
	}

	public void setSellerDAO(SellerDAO sellerDAO) {
		this.sellerDAO = sellerDAO;
	}

	public BookCustomerDAO getBookCustomerDAO() {
		return bookCustomerDAO;
	}

	public void setBookCustomerDAO(BookCustomerDAO bookCustomerDAO) {
		this.bookCustomerDAO = bookCustomerDAO;
	}

	public BookOrderDAO getBookOrderDAO() {
		return bookOrderDAO;
	}

	public void setBookOrderDAO(BookOrderDAO bookOrderDAO) {
		this.bookOrderDAO = bookOrderDAO;
	}

	public ErrorMsg addBookOrder(BookOrder order) throws Exception{
		ErrorMsg msg = new ErrorMsg();
		msg.setCode(Constant.SUCCESS);//success
		//添加创建订单时间
		DateTime dt = new DateTime();
		order.setCreateTime(dt.toString("yyyy-MM-dd hh:mm:ss"));
		//订单号
		order.setOrderNum(dt.toString("yyyy-MM-dd-mm"));
		
		order.setStatus(Constant.TYPE_BOOK_ORDER_INPUT);
		
		int orderId = bookOrderDAO.addBookOrder(order);
		
		Gson gson = new Gson();
		Type type = new TypeToken<List<BookOrderItem>>(){}.getType();
		List<BookOrderItem> items = gson.fromJson(order.getItems(),type);
		
		//批量添加详细信息
		bookOrderDAO.addOrderItems(items, orderId);
		
		return msg;
	}
	
	public ErrorMsg updateBookOrder(BookOrder order) throws Exception{
		ErrorMsg msg = new ErrorMsg();
		msg.setCode(Constant.SUCCESS);//success
		bookOrderDAO.updateBookOrder(order);
		return msg;
	}
	
	public BookCustomer getCustomerByUserId(long userId) throws Exception{
		return bookCustomerDAO.getById(userId);
	}
	
	public void addCustomer(BookCustomer customer) throws Exception{
		if(null == customer)
			return;
		long userId = customer.getUserId();
		if(userId > 0){
			BookCustomer cust = bookCustomerDAO.getById(userId);
			if(null != cust && cust.getUserId() > 0){
				bookCustomerDAO.updateBookCustomer(customer);
			}else{
				bookCustomerDAO.addBookCustomer(customer);
			}
		}
	}
	
	public PageModel getPagedCustomers(PageModel model,String search,long sellerUserId,long sellerId,int type) throws Exception{
		return bookCustomerDAO.getPagedInfos(model, search, sellerUserId,sellerId,type);
	}
	
	public PageModel getPagedOrders(PageModel model,String search,long sellerUserId,long sellerId,String start,String end,int status,int type) throws Exception{
		
		return bookOrderDAO.getPagedInfos(model, search, sellerUserId, sellerId, start, end, status,type);
	}
	
	public PageModel getUserPagedOrders(PageModel model,String search,long userId,long sellerId,String start,String end,int status,int type) throws Exception{
		return bookOrderDAO.getUserPagedInfos(model, search, userId, sellerId, start, end, status,type);
	}
	
	public BookOrder getOrderByOrderId(long orderId) throws Exception{
		BookOrder order = null;
		order = bookOrderDAO.getById(orderId);
		if(null == order)
			return null;
		if(order.getSellerId() > 0){
			//获取商家信息
			order.setSeller(sellerDAO.getById(order.getSellerId()));
		}
		if(order.getUserId() > 0){
			//获取客户信息
			order.setCustomer(bookCustomerDAO.getById(order.getUserId()));
		}
		
		//设置订单详情
		List<BookOrderItem> items = bookOrderDAO.getItemsByOrderId(order.getOrderId());
		
		//统计价格
		int amount = 0;
		float price = 0;
		float priceRebate = 0;
		
		for(BookOrderItem item:items){
			amount += item.getAmount();
			price += item.getPrice();
			priceRebate += item.getPrice() * item.getAmount();
		}
		
		BookOrderStatis statis = new BookOrderStatis();
		statis.setAmount(amount);
		statis.setPrice(price);
		statis.setPriceRebate(priceRebate);
		
		order.setItemList(items);
		order.setStatis(statis);
		
		return order;
	}
}
